package com.ITArmy.service.impl;

import com.ITArmy.beans.Message;
import com.ITArmy.beans.Praise;
import com.ITArmy.beans.User;
import com.ITArmy.common.ServerResponse;
import com.ITArmy.dao.*;
import com.ITArmy.service.PraiseService;
import com.ITArmy.util.CheckMessage;
import com.ITArmy.util.GetUserSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;

@Service
public class PraiseServiceImpl implements PraiseService {
    GetUserSession getUserSession = new GetUserSession();
    @Autowired
    PraiseMapper praiseMapper;
    @Autowired
    BlogMapper blogMapper;
    @Autowired
    UserMapper userMapper;
    @Autowired

    MessageMapper messageMapper;
    @Autowired
    CommentMapper commentMapper;

    //用户给评论点赞
    @Override
    public ServerResponse likeComment(HttpServletRequest request, long commentId) {
        User user = getUserSession.getUser(request);
        //先判断一下是否点过赞
        if (praiseMapper.selectPraiseByCommentAndUid(user.getuId(), commentId) != null) {
            return ServerResponse.createByErrorMesage("您已经点赞过该评论了...");
        }
        Praise praise = new Praise();
        praise.setpTime(new Date());
        praise.setuId(user.getuId());
        praise.setcId(commentId);
        //获取被操作者的id
        praise.setuCid(commentMapper.selectByPrimaryKey(commentId).getuId());
        int resultCount = praiseMapper.insertSelective(praise);
        //将消息储存到消息表中
        Message message = new Message();
        //时间
        message.setTime(new Date());
        //操作者id
        message.setuId(user.getuId());
        //被操作者id
        message.setuCid(commentMapper.selectByPrimaryKey(commentId).getuId());
        System.out.println("被操作者的id为" + commentMapper.selectByPrimaryKey(commentId).getuId());
        //被点赞的评论id
        message.setcId(commentId);
        //点赞id
        praiseMapper.selectPraiseByCommentAndUid(user.getuId(), commentId);
        message.setStatus("1");
        messageMapper.insert(message);
        CheckMessage check = new CheckMessage();
        return check.getMessage(resultCount, "点赞成功", "点赞失败");
    }

    /**
     * 用户给博客点赞
     */
    @Override
    public ServerResponse likeBlog(HttpServletRequest req, long blogId) {
        User user = getUserSession.getUser(req);
        //先查看一下是否点赞过
        if (praiseMapper.selectPraise(user.getuId(), blogId) != null) {
            return ServerResponse.createByErrorMesage("您已经点过赞了...");
        }
        Praise praise = new Praise();
        praise.setbId(blogId);
        praise.setuId(user.getuId());
        praise.setpTime(new Date());
        long uCid = blogMapper.selectByPrimaryKey(blogId).getuId();
        praise.setuCid(uCid);
        int resultCount = praiseMapper.insertSelective(praise);
        //将消息储存到message表中
        Message message = new Message();
        //获取被点赞者的(用户)id
        message.setuCid(uCid);
        System.out.println("被点赞的人的id" + message.getuCid());
        //动作发生者的id用户的id
        message.setuId(user.getuId());
        //信息产生的时间
        message.setTime(new Date());
        //点赞的id
        message.setpId(praiseMapper.selectPraise(user.getuId(), blogId).getpId());
        //添加到message表单里面
        message.setStatus("1");
        messageMapper.insert(message);
        CheckMessage check = new CheckMessage();
        return check.getMessage(resultCount, "点赞成功", "点赞失败");
    }

    /**
     *用户取消点赞评论
     */
    @Override
    public ServerResponse unlikeComment(long cid) {
        //通过评论id查找点赞信息
        Praise praise= praiseMapper.selecePraiseByCid(cid);
        //删除点赞信息
        if (praiseMapper.deleteByPrimaryKey(praise.getpId())==1){
            //删除点赞消息
            int resultCount = messageMapper.deleteMessageByPid(praise.getpId());
            CheckMessage check = new CheckMessage();
            return check.getMessage(resultCount, "取消成功", "取消失败");
        }else {
            return ServerResponse.createByErrorMesage("取消失败");
        }
    }

    /**
     *用户取消点赞博客
     */
    @Override
    public ServerResponse unlikeBlog(long bid) {
        //通过博客id查找点赞信息
        Praise praise= praiseMapper.selecePraiseByBid(bid);
        //删除点赞信息
        if (praiseMapper.deleteByPrimaryKey(praise.getpId())==1){
            //删除点赞消息
            int resultCount = messageMapper.deleteMessageByPid(praise.getpId());
            CheckMessage check = new CheckMessage();
            return check.getMessage(resultCount, "取消成功", "取消失败");
        }else {
            return ServerResponse.createByErrorMesage("取消失败");
        }
    }
}
